package synchronize;

import java.util.concurrent.Callable;

abstract class DeprovisionTask implements Callable<Integer>{

    private Integer id = 0;
    private String taskName = "";

    abstract protected void deprovision();

    public DeprovisionTask(Integer id){
        setId(id);
    }

    @Override
    public Integer call() throws Exception {
        synchronized (DeprovisionTask.class) {
            try {
                for (int i = 0; i < 5; i++) {
                    deprovision();

                }
                return 1;

            } catch (Exception e) {
                return -1;
            }
        }
    }

    public Integer getId() {
        return id;
    }

    protected void setId(Integer id) {
        this.id = id;
    }
    public String getTaskName() {
        return taskName;
    }

    protected void setTaskName(String taskName) {
        this.taskName = taskName;
    }
}

class DeprovisionDomainTask extends DeprovisionTask{

    public DeprovisionDomainTask(Integer id) {
        super(id);
        setTaskName("domain_id=" + id);
    }

    private static int count;

    @Override
    protected void deprovision() {
        System.out.println(getTaskName() + ": " + (count++));

    }
}

public class TestSynchronized {
    public static void main(String[] args) throws Exception {
        for(int i =0; i<5; i++){
            DeprovisionDomainTask deprovisionDomainTask = new DeprovisionDomainTask(i);
            Integer result = deprovisionDomainTask.call();
        }
    }
}
